(function () {
    let app = angular.module("DIBS");
    app.controller("addServiceInfoCtrl", ['$http','$scope', addServiceInfoCtrl]);
    addServiceInfoCtrl.$inject = ['$http'];
    function addServiceInfoCtrl($http,$scope) {
        let vm = this;
        vm.color={
            "background-color":"red"
        };

        vm.updateServiceInfo = function(data,type) {
            $http({
                method: "POST",
                url: "/updateServiceInfo",
                data: {serviceInfo: data, type: type}
            }).success(function (data, status) {
                if(data.resultCode=='01'){
                    vm.message= data.message;
                    vm.color["background-color"] = 'green';
                }else if( data.resultCode=='00'){
                    vm.message=data.message;
                    vm.color["background-color"] = 'red';
                }
                $('#addedServiceModal').modal('show');
            }).error(function (data, status) {
                //vm.data = data || "Request failed";
                //vm.status = status;
                console.log(data)
            });
        };
        vm.addTarget = function(updateTarget,val){
            if(_.isEmpty(val)){
                return;
            }
            let url ='', updateData = {},targetListName ='' ;
            updateData.type = 'New';
            if(updateTarget === 'Domain'){
                url = "/updateDomain";
                updateData.domain = vm.domain;
                targetListName = 'domainList';
                //vm.domain.domainName = '';
            }else{
                url = "/updateServiceType";
                updateData.serviceType = vm.serviceType;
                targetListName = 'serviceTypeForSelect';
                //vm.serviceType.serviceTypeName = ''
            }
            $http({
                method: "POST",
                url: url,
                data: updateData
            }).success(function (data, status) {
                $scope[targetListName].push(val);
                if(data.resultCode === '01'){
                    vm.message= data.message;
                }else if( data.resultCode === '00'){
                    vm.message=data.message;
                    vm.color["background-color"] = 'red';
                    $('#addedServiceModal').modal('show');
                }
            }).error(function (data, status) {
                vm.message=data.message;
                vm.color["background-color"] = 'red';
                $('#addedServiceModal').modal('show');
            });
        };
        vm.deleteTarget = function(deleteTarget, deleteTargetName){
            let url ='' ;
            if(deleteTarget === 'Domain'){
                url = "/updateDomain";
                let removeList = $scope.domainList;
            }else{
                url = "/updateServiceType";
                let removeList = $scope.serviceTypeForSelect;
            }
            $http({
                method: "POST",
                url: url,
                data:{name:deleteTargetName,type:'Delete'}
            }).success(function (data, status) {
                _.remove(removeList,function(rec){
                    return rec === deleteTargetName;
                });
            }).error(function (data, status) {
                console.log(data)
            });
        }


    }
})();